﻿using System;
using System.Drawing;
using Microsoft.VisualBasic.PowerPacks;
using System.Windows.Forms.VisualStyles;
using System.Windows.Forms;
using G.Model.Runtime;
using G.Runtime;
using GameLibrary.Model.Core;

namespace GameDesigner.Source.Widgets
{
    public class ButtonWidget : Updatable, IDisposable 
    {
        private readonly Game _Game;
        private readonly GameTask _GameTask;
        private readonly GameElement _GameElement;
        private readonly ButtonPanel _Painter;
        private readonly SimpleShape _Shape;
        private readonly string _Text;
        
        private Point _Point = new Point(10, 10);
        private bool _Pressed;

        public ButtonWidget(GameElement pGameElement, ButtonPanel painter,
            SimpleShape control, Game pGame,  GameTask pGameTask, string text = "")
        {
            _GameElement = pGameElement;
            _Text = text;
            _Shape = control;
            _Painter = painter;
            _GameTask = pGameTask;
            _Game = pGame;
            _Shape.MouseMove += this.OnMouseMove;
            _Shape.Click += this.OnClick;
        }

        public void Dispose()
        {
            _Shape.MouseMove -= this.OnMouseMove;
            _Shape.Click -= this.OnClick;
        }

        public Point Point
        {
            get { return _Point; }
            set { _Point = value; }
        }

        private void OnClick(object sender, EventArgs e)
        {
            MouseEventArgs args = (MouseEventArgs)e;

            Rectangle r = new Rectangle(_Point, GetSize());
            if (r.Contains(new Rectangle(args.Location, new Size(1, 1))))
            {
                _GameTask.Triggers.FireTriggerEvent(_GameElement);
                _Game.Elements.FireElementMoved(_GameElement);
                _Game.Elements.Actions.Clear();
                
                _Painter.Remove(this);
            }
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            Rectangle r = new Rectangle(_Point, GetSize());
            if (r.Contains(new Rectangle(e.Location, new Size(1, 1))))
            {
                SetPressedFlag(true);
            }
            else
            {
                SetPressedFlag(false);
            }
        }

        private void SetPressedFlag(bool pressed)
        {
            if (_Pressed != pressed)
            {
                _Pressed = pressed;
                _Shape.Refresh();
            }
        }

        public void Update(Graphics graphics) 
        {

            VisualStyleRenderer renderer;
            if (_Pressed)
            {
                renderer = new VisualStyleRenderer(
                    VisualStyleElement.Button.PushButton.Pressed);
            }
            else
            {
                renderer = new VisualStyleRenderer(
                    VisualStyleElement.Button.PushButton.Normal);
            }
            renderer.DrawBackground(graphics, GetRectangle());

            Font fn = new Font("Comic Sans MS", 12);
            graphics.DrawString(_Text, fn, Brushes.Black, GetPoint());
        }

        private Point GetPoint()
        {
            return new Point(_Shape.Location.X + _Point.X, _Shape.Location.Y + _Point.Y);
        }

        private Rectangle GetRectangle()
        {
            return new Rectangle(GetPoint(), GetSize());
        }

        private static Size GetSize()
        {
            return new Size(100, 25);
        }
    }

}
